from typing import List, Dict


class ContextManager:
    """
    对话上下文管理器,用于记录用户输入的message等内容信息
    """

    def __init__(self):
        self.history: List[Dict] = []

    @staticmethod
    def add_message(role: str, content: str):
        """
        添加message
        :param role: USER/ASSISTANT
        :param content: 用户输入内容/LLM生成内容
        :return:
        """
        context_manager.history.append({"role": role, "content": content})
        if len(context_manager.history) > 10 * 2:  # 最多保留10轮对话
            context_manager.history = context_manager.history[-10 * 2:]


context_manager = ContextManager()
